package com.centit.framework.ip.businessdata.controller;

import com.alibaba.fastjson.JSONArray;
import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.ip.businessdata.po.InterfaceLog;
import com.centit.framework.ip.businessdata.service.InterfaceLogManager;
import com.centit.support.database.utils.PageDesc;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * InterfaceLog  Controller.
 * create by scaffold 2018-05-22
 *
 * @author guo_jh
 * T_INTERFACE_LOG接口调用日志
 */


@Controller
@RequestMapping("/interfacemanager/interfacelog")
public class InterfaceLogController extends BaseController {
    private static final Log log = LogFactory.getLog(InterfaceLogController.class);

    @Resource
    private InterfaceLogManager interfaceLogMag;


    /**
     * 查询所有   T_INTERFACE_LOG  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public void list(String[] field, PageDesc pageDesc, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> searchColumn = convertSearchColumn(request);

        JSONArray jsonArrayObjects = interfaceLogMag.listObjectsAsJson(searchColumn, pageDesc);

        if (null == pageDesc) {
            JsonResultUtils.writeSingleDataJson(jsonArrayObjects, response);
            return;
        }

        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArrayObjects);
        resData.addResponseData(PAGE_DESC, pageDesc);

        JsonResultUtils.writeResponseDataAsJson(resData, response);
    }

    /**
     * 查询单个  T_INTERFACE_LOG
     *
     * @param recordid RECORDID
     * @param response {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{recordid}", method = {RequestMethod.GET})
    public void getInterfaceLog(@PathVariable String recordid, HttpServletResponse response) {

        InterfaceLog interfaceLog =
                interfaceLogMag.getObjectById(recordid);

        JsonResultUtils.writeSingleDataJson(interfaceLog, response);
    }

    /**
     * 新增 T_INTERFACE_LOG，业务只有新增的时候可用此方法，如有修改可用下面 新增或保存合并的方法
     *
     * @param interfaceLog {@link InterfaceLog}
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST})
    public void createInterfaceLog(@Valid InterfaceLog interfaceLog, HttpServletResponse response) {
        interfaceLogMag.saveNewObject(interfaceLog);
        JsonResultUtils.writeSingleDataJson("操作成功", response);
    }

    /**
     * 删除单个  T_INTERFACE_LOG
     *
     * @param recordid RECORDID
     */
    @RequestMapping(value = "/{recordid}", method = {RequestMethod.DELETE})
    public void deleteInterfaceLog(@PathVariable String recordid, HttpServletResponse response) {

        interfaceLogMag.deleteObjectById(recordid);

        JsonResultUtils.writeSingleDataJson("操作成功", response);
    }

    /**
     * 新增或保存 T_INTERFACE_LOG
     *
     * @param recordid     RECORDID
     * @param interfaceLog {@link InterfaceLog}
     * @param response     {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{recordid}", method = {RequestMethod.PUT})
    public void updateInterfaceLog(@PathVariable String recordid,
                                   @Valid InterfaceLog interfaceLog, HttpServletResponse response) {


        InterfaceLog dbInterfaceLog =
                interfaceLogMag.getObjectById(recordid);


        if (null != dbInterfaceLog) {//如果数据库数据存在，则是更新操作
            dbInterfaceLog.copyNotNullProperty(interfaceLog);//赋值页面不为空的值，小心页面空的字符串或者改写copyNotNullProperty方法，根据自己业务需要
            interfaceLogMag.mergeObject(dbInterfaceLog);
        } else {
            interfaceLogMag.saveNewObject(interfaceLog);//新增,注意主键赋值 UuidUtil.getTimeBasedUuid()，填写自己的业务时间，业务操作人等信息
        }

        JsonResultUtils.writeSingleDataJson("操作成功", response);
    }
}
